package ee.helmesWorkTest.backend.model.dao.postgresqlimplementations;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
import ee.helmesWorkTest.backend.database.ConnectionManager;
import ee.helmesWorkTest.backend.model.dao.PersonDAO;
import ee.helmesWorkTest.backend.model.data.Person;

public class PersonDAOPostgresqlImpl implements PersonDAO
{
	Logger logger = Logger.getLogger(PersonDAOPostgresqlImpl.class);
	Connection dbConnection;
	String sql;
	PreparedStatement preparedStatement;
	Statement statement;
	ResultSet resultSet;
	private final int WARNING_NO_ROWS_AFFECTED = 0;
	private int rowsAffected;
	
	public PersonDAOPostgresqlImpl(Connection dbConnection)
	{
		this.dbConnection = dbConnection;
	}
	
	@Override
	public int insertPerson(Person person) 
	{
		resultSet = null;
		rowsAffected = WARNING_NO_ROWS_AFFECTED;
		sql = "INSERT INTO Persons " +  
				"(name) " +  
				"VALUES " + 
				"(?);";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			preparedStatement.setString	(1, person.getName());
			logger.info(preparedStatement.toString());
			rowsAffected = preparedStatement.executeUpdate();
			
			if (rowsAffected == 1)
			{
				resultSet = preparedStatement.getGeneratedKeys();
				resultSet.next();
				int newId = resultSet.getInt(1);
				person.setPersonID(newId);
				return rowsAffected;
			}
			else
			{
				System.out.println("No rows affected!");
				logger.info("Unsuccessful insertion to a database: " + preparedStatement.toString());
				return WARNING_NO_ROWS_AFFECTED;
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			return WARNING_NO_ROWS_AFFECTED;
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
	}

	@Override
	public Person getPersonByID(int personID) 
	{
		resultSet = null;
		Person personRetrieved = null;
		sql = "SELECT * FROM Persons WHERE person_id = ?;";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(
						sql,
						ResultSet.TYPE_SCROLL_INSENSITIVE,
						ResultSet.CONCUR_READ_ONLY
					);
			preparedStatement.setInt(1, personID);
			logger.info(preparedStatement.toString());
			resultSet = preparedStatement.executeQuery();
			resultSet.beforeFirst();
			
			if (resultSet.next())
			{
				personRetrieved = new Person();
				personRetrieved.setPersonID(resultSet.getInt("person_id"));
				personRetrieved.setName(resultSet.getString("name"));
			}
		}
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
		
		return personRetrieved;
	}

	@Override
	public int updatePerson(Person person) 
	{
		resultSet = null;
		rowsAffected = WARNING_NO_ROWS_AFFECTED;
		sql = "UPDATE Persons " + 
				"SET name = ? " + 
				"WHERE person_id = ?;"; 
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(sql);
			
			preparedStatement.setString	(1, person.getName());
			preparedStatement.setInt	(2, person.getPersonID());

			rowsAffected = preparedStatement.executeUpdate();
			
			if (rowsAffected == 1)
			{
				logger.info("Update sucessful: " + preparedStatement.toString());
				return rowsAffected;
			}
			else
			{
				logger.error("Update was unsuccesful: " + preparedStatement.toString());
				return WARNING_NO_ROWS_AFFECTED;
			}
		}
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
			return WARNING_NO_ROWS_AFFECTED;		
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
	}
}
